package com.by.common.crud;

import cn.hutool.core.map.MapUtil;
import com.by.common.bean.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.*;

/**
 * 默认实现
 *
 * @param <T>
 */
public class BaseServiceImpl<T, M extends BaseMapper> implements BaseService<T> {


    @Autowired(required = false)
    protected M baseMapper;

    @Override
    public PageBean query(Map map) {
        int page = MapUtil.getInt(map,"page");
        int pageSize = MapUtil.getInt(map,"pageSize");
        //设置分页
        PageHelper.startPage(page, pageSize);// 后面紧跟查询
        List<Map> list = baseMapper.query(map);

        //获取分页信息 将查询结果封装一下
        PageInfo info = new PageInfo(list);

        return new PageBean(page, pageSize, info.getTotal(), list);
    }

    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }

    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) baseMapper.get(id);
    }
}
